
Alkaline.Processing.ValueDistribution = function(initList) {
	this.propertyDescriptors = [];
	if (!initList) {
		this.conditions = [];
		this.values = [];
	} else if (initList instanceof Alkaline.Processing.ValueDistribution) {
		this.conditions = [].concat(initList.conditions);
		this.values = [].concat(initList.values);
	} else if (!("length" in initList)) {
		this.conditions = initList.conditions || [ initList.condition ];
		this.values = initList.values || [ initList.value ];
	} else {
		this.conditions = [];
		this.values = [];
		for (var i = 0; i < initList.length; i++) {
			this.conditions.push(initList[i].condition);
			this.values.push(initList[i].value);
		}
	}
}

Alkaline.Processing.ValueDistribution.prototype.setValue = function(condition, newValue) {
	if (condition === Alkaline.Processing.Condition.NEVER)
		return;
	var originalCondition = null;
	if (newValue instanceof Alkaline.Processing.ValueDistribution) {
		originalCondition = condition;
		for (var i = 0; i < newValue.values.length; i++)
			condition = condition.or(newValue.conditions[i]);
	}
	for (var i = 0; i < this.conditions.length; i++)
		if (condition.includes(this.conditions[i])) {
			this.conditions.splice(i, 1);
			this.values.splice(i, 1);
			i--;
		}
	if (newValue instanceof Alkaline.Processing.ValueDistribution) {
		this.conditions = newValue.conditions.concat(this.conditions);
		this.values = newValue.values.concat(this.values);
	} else {
		this.conditions.splice(0, 0, condition);
		this.values.splice(0, 0, newValue);
	}
}

Alkaline.Processing.ValueDistribution.prototype.getValue = function(condition) {
	var values = [];
	for (var i = 0; i < this.values.length; i++) {
		var c = this.conditions[i].and(condition);
		if (c !== Alkaline.Processing.Condition.NEVER) {
			values.push({ condition: c, value: this.values[i] });
			condition = condition.andNot(this.conditions[i]);
		}
	}
	return new Alkaline.Processing.ValueDistribution(values);
}

Alkaline.Processing.ValueDistribution.prototype.getPropertyDescriptor = function(condition, name) {
	var p = Alkaline.Processing;
	var conditions = [], values = [];
	for (var i = 0; i < this.conditions.length; i++) {
		var cond = this.conditions[i].and(condition);
		if (cond !== p.Condition.NEVER) {
			var list = this.values[i].getPropertyDescriptor(cond, name);
			conditions.addAll(list.conditions);
			values.addAll(list.values);
		}
	}
	return new p.ValueDistribution({ conditions: conditions, values: values });
}

Alkaline.Processing.ValueDistribution.prototype.equals = function(condition, value) {
	var vd = Alkaline.Processing.ValueDistribution;
	if (value instanceof vd) {
		if (this.values.length !== value.values.length)
			return false;
		for (var i = 0; i < this.values.length; i++)
			if (this.conditions[i] !== value.conditions[i] || !this.values[i].equals(value.values[i]))
				return false;
		return true;
	} else {
		if (this.values.length === 1)
			return this.values[0].equals(value);
		return false;
	}
}
